package com.hzcx.app.simplechat.ui.chat;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.api.BaseObserver;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.base.BaseEmptyBean;
import com.hzcx.app.simplechat.model.GroupModel;
import com.hzcx.app.simplechat.ui.friend.FriendListByLableActivity;
import com.hzcx.app.simplechat.ui.friend.bean.FriendBean;
import com.hzcx.app.simplechat.ui.group.GroupCreateActivity;
import com.hzcx.app.simplechat.ui.group.GroupJoinApplyActivity;
import com.hzcx.app.simplechat.ui.group.GroupMangersActivity;
import com.hzcx.app.simplechat.ui.group.GroupOwnerTransferActivity;
import com.hzcx.app.simplechat.ui.group.GroupMuteSettingActivity;
import com.hzcx.app.simplechat.ui.group.LiveData.GroupInfoBeanLiveData;
import com.hzcx.app.simplechat.ui.group.adapter.GroupMembersSort;
import com.hzcx.app.simplechat.ui.group.bean.GroupInfoBean;
import com.hzcx.app.simplechat.ui.group.bean.MembersBean;
import com.hzcx.app.simplechat.ui.publicui.PublicConfirmDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 群组管理设置
 */
public class ChatGroupManagerSettingActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.group_manager_join_type)
    TextView groupManagerJoinType;
    @BindView(R.id.group_manager_search_type)
    TextView groupManagerSearchType;
    @BindView(R.id.group_manager_qr)
    Switch groupManagerQr;
    @BindView(R.id.group_manager_member_limit)
    TextView groupManagerMemberLimit;
    @BindView(R.id.group_manager_member_protect)
    Switch groupManagerMemberProtect;
    @BindView(R.id.group_manager_mute)
    TextView groupManagerMute;
    @BindView(R.id.group_manager_robot)
    TextView groupManagerRobot;
    @BindView(R.id.group_manager_admin)
    TextView groupManagerAdmin;
    @BindView(R.id.group_manager_admin_transfer)
    TextView groupManagerAdminTransfer;
    @BindView(R.id.group_manager_join_type_content)
    TextView groupManagerJoinTypeContent;
    @BindView(R.id.group_manager_search_type_des)
    TextView groupManagerSearchTypeDes;
    @BindView(R.id.group_manager_admin_count)
    TextView groupManagerAdminCount;



//    public static String GroupInfoBeanKey = "ChatGroupManagerSettingActivityGroupInfoBean";
//    GroupInfoBean groupInfoBean;
    public static String GroupInfoBeanKeyGroupID = "QrCodeActivity_GroupInfoBeanKeyGroupID";
    GroupInfoBean groupInfoBean;
    private String groupID;

    @Override
    public int getLayout() {
        return R.layout.activity_chat_group_manager_setting;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        tvTitle.setText("群管理");
    }

    @Override
    protected void onResume() {
        super.onResume();
        //        groupInfoBean = (GroupInfoBean)getIntent().getSerializableExtra(GroupInfoBeanKey);
        groupID = getIntent().getStringExtra(GroupInfoBeanKeyGroupID);
        groupInfoBean = GroupInfoBeanLiveData.getGroupInfoBean(groupID);
        reStatus(groupInfoBean);
    }

    private void reStatus(GroupInfoBean groupInfoBean) {
        if (groupInfoBean == null) { return; }
        groupManagerMemberProtect.setChecked(groupInfoBean.getProtectedswitch() != 0);
        groupManagerJoinTypeContent.setText(groupInfoBean.getJoin_mode_text_zh_cn());

        if (groupInfoBean.getGroup_property().equals("Y")) {
            groupManagerSearchTypeDes.setText(R.string.group_search_id_key);
        } else if (groupInfoBean.getGroup_property().equals("F")) {
            groupManagerSearchTypeDes.setText(R.string.group_search_id);
        } else {
            groupManagerSearchTypeDes.setText("不可被查找");
        }
        groupManagerQr.setChecked(groupInfoBean.getQrcodeswitch() != 0);
        if (groupInfoBean.getMy()!=null&&groupInfoBean.getMy().getIs_owner().equals("Y")) {
            groupManagerAdminTransfer.setVisibility(View.VISIBLE);
        } else {
            groupManagerAdminTransfer.setVisibility(View.GONE);
        }
        int adminCount = 0;
        for (MembersBean bean : groupInfoBean.getMembers()) {
            if (bean.getIs_admin().equals("Y")) {
               adminCount++;
            }
        }
        groupManagerAdminCount.setText(adminCount + "人");
    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.tv_title_right, R.id.group_manager_join_type, R.id.group_manager_search_type, R.id.group_manager_qr,
            R.id.group_manager_member_limit, R.id.group_manager_member_protect, R.id.group_manager_mute, R.id.group_manager_robot,
            R.id.group_manager_admin, R.id.group_manager_admin_transfer, R.id.group_manager_join_apply})
    public void viewOnClick(View view) {

        switch (view.getId()) {
            case R.id.group_manager_join_type:
                Intent intent = new Intent(this, GroupJoinSettingActivity.class);
                intent.putExtra(GroupJoinSettingActivity.GroupInfoBeanKeyGroupID, groupInfoBean.getGroup_id());
                startActivity(intent);
                break;
            case R.id.group_manager_search_type:
                Intent intentSearch = new Intent(this, GroupSettingSearchActivity.class);
                intentSearch.putExtra(GroupSettingSearchActivity.GroupInfoBeanKeyGroupID, groupInfoBean.getGroup_id());
                startActivity(intentSearch);
                break;
            case R.id.group_manager_qr:
                GroupModel.modifyQrGroup(this, groupInfoBean.getGroup_id(), new BaseObserver<BaseEmptyBean>() {
                    @Override
                    protected void onSuccess(BaseEmptyBean data) {
                        groupInfoBean.setQrcodeswitch(groupManagerQr.isChecked() ? 1 : 0);
                    }
                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        super.onFailure(e, errorMsg);
                    }
                });
                break;
            case R.id.group_manager_member_protect:
                GroupModel.modifyGroupProtect(this, groupInfoBean.getGroup_id(), new BaseObserver<BaseEmptyBean>() {
                    @Override
                    protected void onSuccess(BaseEmptyBean data) {

                    }
                });
                break;

            case R.id.group_manager_member_limit:
                groupLimitTip();
                break;

            case R.id.group_manager_mute:
                groupMuteSetting();
                break;

            case R.id.group_manager_join_apply:
                getInviteMembers();
                break;

            case R.id.group_manager_admin:
                gotoGroupManager();
                break;

            case R.id.group_manager_admin_transfer:
                gotoTransferGroupOwner();
                break;
        }
    }

    private void gotoTransferGroupOwner() {
        List<FriendBean> beans = GroupMembersSort.sortFriend(groupInfoBean.getMembers());
        Intent intent = new Intent(this, GroupOwnerTransferActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(FriendListByLableActivity.INTENT_LIST_BUNDLE, beans.toArray());
        intent.putExtras(bundle);
        intent.putExtra(GroupCreateActivity.GroupCreateActivityGroupID, groupInfoBean.getGroup_id());
        startActivity(intent);

    }

    private void gotoGroupManager() {
        Intent intent = new Intent(this, GroupMangersActivity.class);
        intent.putExtra(GroupMangersActivity.GroupInfoBeanKeyGroupID, groupInfoBean.getGroup_id());
        startActivity(intent);
    }

    private void getInviteMembers() {
        Intent intent = new Intent(this, GroupJoinApplyActivity.class);
        intent.putExtra(GroupJoinApplyActivity.GroupInfoBeanKeyGroupID, groupInfoBean.getGroup_id());
        startActivity(intent);
    }

    private void groupMuteSetting() {
        Intent intent = new Intent(this, GroupMuteSettingActivity.class);
        intent.putExtra(GroupMuteSettingActivity.GroupInfoBeanKeyGroupID, groupInfoBean.getGroup_id());
        startActivity(intent);
    }

    /**
     *  群规模
     */
    private void groupLimitTip() {
        showConfirmDialog("", "专业版群规模可提升至500人，有更多 定制化服务~想要了解更多？", null, new PublicConfirmDialog.OnPublicConfirmClickListener() {
            @Override
            public void publicConfirm() {
                //TODO:
                // do action
//                showLoading();
//                new Thread(() -> {
//                    EMClient.getInstance().chatManager().deleteConversation(groupInfoBean.getHx_groupid(), true);
//                    handler.sendEmptyMessage(HANDLER_ChatGroup_Message_DELETE);
//                }).start();
            }
        });
    }

}
